package com.health.es.writer;

import cn.hutool.core.collection.CollUtil;
import com.health.es.service.ConvertService;
import com.health.es.temp.EsClassOperations;
import com.health.trans.pojo.JobConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.item.ItemWriter;

import java.util.List;

/**
 * @author nqlz
 */
@Slf4j
public class MySqlToEsWriter<T> implements ItemWriter<T> {

    private final JobConfig<T> jobConfig;
    private final Class<T> aClass;

    public MySqlToEsWriter(JobConfig<T> jobConfig) {
        this.jobConfig = jobConfig;
        this.aClass = EsClassOperations.getEsClass(jobConfig.getWriteTableClass().getSimpleName());
    }

    @Override
    public  void write(List<? extends T> list) throws Exception {
        if (CollUtil.isNotEmpty(list)) {
            ConvertService.verseAndSaveObjs(aClass, list);
            jobConfig.log(list.size());
        }
    }


}
